package spe;

import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;

import spe.ui.RCPUIText;
import spe.ui.wizard.NewWizardMenu;
import spe.ui.action.OpenViewAction;

/**
 * @author Dao Anh Vu - daoanhvu122@gmail.com
 * @date May 28, 2012
 * 
 * @description This class creates, add and dispose menu on workbench, thanks to
 * 		Kelvin Low, Bingxue Xu,Phong Nguyen Le and Jinhua Xi from project EPF at IBM
 * 
 * */

public class ApplicationActionBarAdvisor extends ActionBarAdvisor 
{
	private IWorkbenchWindow window;
	
	//Action for File menu
	protected IWorkbenchAction newAction;
	protected IWorkbenchAction openAction;
	protected IWorkbenchAction closeAction;
	protected IWorkbenchAction closeAllAction;
	protected IWorkbenchAction exitAction;
	protected IWorkbenchAction newWizardDropDownAction;
	
	//Action for Edit menu
	protected IWorkbenchAction copyAction;
	protected IWorkbenchAction cutAction;
	protected IWorkbenchAction pasteAction;
	
	//Action for Help menu
	protected IWorkbenchAction helpAction;
	protected IWorkbenchAction aboutAction;
	
	private MenuManager mainFileMenuMgr, mainHelpMenuMgr;

    public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) 
    {
        super(configurer);
        window = configurer.getWindowConfigurer().getWindow();
    }
    
    public IWorkbenchWindow getWindow()
    {
    	return window;
    }

    /**
     * Create actions and register them. See plugin.xml to for more information about
     * binding commands to actions here
     * */
    protected void makeActions(IWorkbenchWindow window) 
    {
    	if(PlatformUI.getWorkbench().isClosing())
    		return;
    	
    	newAction = ActionFactory.NEW.create(window);
    	register(newAction);
    	
    	//openAction = ActionFactory.OP
    	
    	closeAction = ActionFactory.CLOSE.create(window);
    	register(closeAction);
    	
    	closeAllAction = ActionFactory.CLOSE_ALL.create(window);
    	register(closeAllAction);
    	
    	exitAction = ActionFactory.QUIT.create(window);
    	register(exitAction);
    	
    	copyAction = ActionFactory.COPY.create(window);
    	cutAction = ActionFactory.CUT.create(window);
    	pasteAction = ActionFactory.PASTE.create(window);
    	
    	helpAction = ActionFactory.HELP_CONTENTS.create(window);
    	aboutAction = ActionFactory.ABOUT.create(window);
    	
    	// FIXME! Filter out Project... from the drop-down menu.
    	//newWizardDropDownAction = IDEActionFactory.NEW_WIZARD_DROP_DOWN.create(window);
    	newWizardDropDownAction = ActionFactory.NEW_WIZARD_DROP_DOWN.create(window);
    	register(newWizardDropDownAction);
    }

    protected void fillMenuBar(IMenuManager menuBar) 
    {
    	super.fillMenuBar(menuBar);
    	if(PlatformUI.getWorkbench().isClosing())
    		return;
    	
    	mainFileMenuMgr = createFileMenu(menuBar);
		menuBar.add(mainFileMenuMgr);
		
		menuBar.add(createEditMenu(menuBar));
		menuBar.add(createWindowMenu(menuBar));

		//IMenuManager navManager = createNavigateMenu(menuBar);
		//menuBar.add(navManager);
		//navManager.setVisible(false);

		//menuBar.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		//menuBar.add(createWindowMenu(menuBar));

		mainHelpMenuMgr = createHelpMenu(menuBar);
		menuBar.add(mainHelpMenuMgr); 
    }
    
    private MenuManager createFileMenu(IMenuManager menuBar)
    {
    	MenuManager fileMenuMgr = new MenuManager(RCPUIText.getText("menu.file.text"), IWorkbenchActionConstants.M_FILE);
    	fileMenuMgr.add(new GroupMarker(IWorkbenchActionConstants.FILE_START));
    	
    	//Create new sub menu for "New"
    	String newText = RCPUIText.getText("menu.file.new");
    	String newId = ActionFactory.NEW.getId();
    	MenuManager newMenuMgr = new MenuManager(newText, newId){
    		public String getMenuText()
    		{
    			return super.getMenuText();
    		}
    	};
    	
    	newMenuMgr.add(new Separator(newId));
    	newMenuMgr.add(new NewWizardMenu(getWindow(), null));
    	newMenuMgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
    	fileMenuMgr.add(newMenuMgr);
    	
    	fileMenuMgr.add(new Separator(IWorkbenchActionConstants.NEW_EXT));
    	
    	fileMenuMgr.add(new Separator());
		MenuManager openSubMenu = new MenuManager(RCPUIText.getText("menu.file.open.text"),	IWorkbenchActionConstants.OPEN_EXT);
		openSubMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
		openSubMenu.add(new Separator());
		fileMenuMgr.add(openSubMenu);
		
		fileMenuMgr.add(new Separator());
		fileMenuMgr.add(closeAction);
		fileMenuMgr.add(closeAllAction);
		
		fileMenuMgr.add(new Separator());
		fileMenuMgr.add(exitAction);
		
		fileMenuMgr.add(new GroupMarker(IWorkbenchActionConstants.FILE_END));
		
    	return fileMenuMgr;
    }
    
    private MenuManager createEditMenu(IMenuManager menuBar)
    {
    	MenuManager eidtMenuMgr = new MenuManager(RCPUIText.getText("menu.edit.text"), IWorkbenchActionConstants.M_EDIT);
    	eidtMenuMgr.add(new GroupMarker(IWorkbenchActionConstants.EDIT_START));
    	
    	eidtMenuMgr.add(copyAction);
    	eidtMenuMgr.add(cutAction);
    	eidtMenuMgr.add(pasteAction);
    	
    	eidtMenuMgr.add(new GroupMarker(IWorkbenchActionConstants.EDIT_END));
    	
    	return eidtMenuMgr;
    }
    
    /*
    private MenuManager createNavigateMenu(IMenuManager menuBar)
    {
    	return null;
    }
    */
    
    private MenuManager createWindowMenu(IMenuManager menuBar)
    {
    	MenuManager windowMgr = new MenuManager(RCPUIText.getText("menu.windows.text"), IWorkbenchActionConstants.M_WINDOW);
    	//windowMgr.add(new GroupMarker(IWorkbenchActionConstants.WB_START));
    	
    	windowMgr.addMenuListener(new IMenuListener(){

			@Override
			public void menuAboutToShow(IMenuManager manager) 
			{
				MenuManager mm = new MenuManager("Show View");
				IWorkbenchPage page = getWindow().getActivePage();
				mm.add(new OpenViewAction(page, "spe.ui.navigator.view", "SPE Explorer"));
				mm.add(new OpenViewAction(page, "spe.ui.view.ProjectExplorer", "Project Explorer"));
				mm.add(new OpenViewAction(page, "org.eclipse.jdt.ui.PackageExplorer", "Package Explorer"));
				mm.add(new OpenViewAction(page, IPageLayout.ID_OUTLINE, "Outline Viewer"));
				mm.add(new OpenViewAction(page, IPageLayout.ID_PROP_SHEET, "Properties"));
				manager.add(mm);
			}
    		
    	});
    	windowMgr.setRemoveAllWhenShown(true);
    	
    	//windowMgr.add(new GroupMarker(IWorkbenchActionConstants.WB_END));
    	return windowMgr;
    }
    
    private MenuManager createHelpMenu(IMenuManager menuBar)
    {
    	MenuManager helpMenuMgr = new MenuManager(RCPUIText.getText("menu.help.text"), IWorkbenchActionConstants.M_HELP);
    	helpMenuMgr.add(new GroupMarker(IWorkbenchActionConstants.HELP_START));
    	
    	helpMenuMgr.add(helpAction);
    	helpMenuMgr.add(aboutAction);
    	
    	helpMenuMgr.add(new GroupMarker(IWorkbenchActionConstants.HELP_END));
    	
    	return helpMenuMgr;
    }
}
